package EEG;



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.URL;
import java.util.Arrays;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.Timer;

import speech.gui.MakeFrames;
import speech.spectral.FeatureClient;
import speech.spectral.NNSpectralFeatureDetector;
import speech.spectral.RealTimeAudioSource;
import speech.spectral.SampledToSpectral;
import uk.org.toot.audio.core.AudioBuffer;
import uk.org.toot.audio.core.AudioProcess;
import uk.org.toot.audio.server.AudioClient;


import EEG.EEGSource;

import com.frinika.audio.io.AudioReader;
import com.frinika.audio.io.VanillaRandomAccessFile;

import config.Config;

public class MonitorMain implements ActionListener{

	private MakeFrames frames;
	private Timer timer;
	private NNSpectralFeatureDetector nnFeatureDetector;
	public boolean isApplet = false; // hack hack hack ... eeeek

	double output[];
	public EEGSource realTimeSpectralSource;
	public SampledToSpectral spectralConverter;
	private Config config;
	int fftSize;
	float sampleRate;
	int outSize;
	private EEGGraph eegGraph;

	String chnNames[]={"1", "2", "3"};    // proof of concept hack 3 channels

	
	public static void main(String args[]) {
		MonitorMain app;
		try {
			app = new MonitorMain();
			app.start();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.exit(-1);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.exit(-1);

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.exit(-1);

		}

	}

	MonitorMain() throws IOException {

		config = Config.current();
		
		eegGraph=new EEGGraph(chnNames);
		
		JFrame frame=new JFrame();
		frame.setContentPane(eegGraph);
		frame.addKeyListener(eegGraph.keyHandler);
		frame.setSize(680, 400);
		
		/*
		 * JButton.
		 */
		JButton startButton = new JButton("Start Logging");//The JButton name.
		JButton stopButton = new JButton("Stop Logging");
		
		frame.add(startButton);//Add the button to the JFrame.
		frame.add(stopButton);
		
		startButton.addActionListener(this);//Reads the action.
		stopButton.addActionListener(this);
		
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}

	/* 
	 *What the button does.
	 */
	public void actionPerformed(ActionEvent e) {
		
		System.out.println("The Button Works!");//what the button says when clicked.
	}
	
	void start() throws InterruptedException, IOException,
			ClassNotFoundException {
		
		
		EEGSource source = new EEGSource();
		
		AudioProcess client = new AudioProcess(){

			@Override
			public void close() throws Exception {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void open() throws Exception {
				// TODO Auto-generated method stub
				
			}

			@Override
			public int processAudio(AudioBuffer arg0) {
				
		
				float vals[]=new float[chnNames.length];
				float buff[]=arg0.getChannel(0);
				
				for(int i=0;i<arg0.getSampleCount();i++){
					for (int j=0;j<chnNames.length;j++){
						vals[j]=buff[i];
					}
					//System.out.print(buff[i]+ " ");
					
					eegGraph.updateGraph(vals);
				}
				//System.out.println();
				eegGraph.repaint();
				
				return 0;
			}

		};
		
		
		source.startAudio(client,config);
		
	}

}